<?php
/**
 * BlogController
 * 
 * @author
 * @version 
 */
require_once('Abstract.php');

class Admin_BlogController extends Admin_AbstractController
{
    /** @var Application_Model_Resource_Blog_Category_Table*/
    protected $categoryResource;
    
    /** @var Application_Model_Resource_Blog_Post_Table*/
    protected $blogResource;

    public function init()
    {
        parent::init();
        $this->categoryResource = Application_Model_Blog_Category::getResource();
        $this->blogResource = Application_Model_Blog::getResource();
    }

    /**
     * The default action - show the home page
     */
    public function indexAction ()
    {
        $this->_forward('list');
    }

    public function listAction()
    {
        $this->view->blogs = $this->blogResource->fetchAll();
    }

    public function addCategoryAction()
    {
        if ($this->getRequest()->isPost()) {     
            try {
                $data = $this->getRequest()->getPost();
                $category = $this->categoryResource->createRow();
                $category->setFromArray($data);
                $category->save();
                $this->_flashMessenger->addMessage("分类创建成功.");
                $this->_redirect(
                $this->view->url2(array('action' => 'list-category')));
            } catch (Exception $e) {
                $this->view->flashMessages[] = "分类创建失败, " . $e->getMessage();
            }
        }
        $this->view->parents = $this->categoryResource->fetchAll();
    }

    public function editCategoryAction()
    {
        $category = $this->categoryResource->findById($this->_getParam('id'));
        if (empty($category)) {
            throw new Application_Model_Blog_Exception('分类不存在.');
        }

        if ($this->getRequest()->isPost()) {
            try {
                $data = $this->getRequest()->getPost();
                $category->setFromArray($data);
                $category->save();
                $this->_flashMessenger->addMessage("分类修改成功.");
                $this->_redirect(
                $this->view->url2(array('action' => 'list-category')));
            } catch (Exception $e) {
                $this->view->flashMessages[] = "分类修改失败, " . $e->getMessage();
            }
        }
        $this->view->category = $category;
        $this->view->parents = $this->categoryResource->fetchAll();
    }

    public function listCategoryAction()
    {
        $this->view->categories = $this->categoryResource->fetchAll();
    }

    public function delCategoryAction()
    {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $category = $this->categoryResource->findById($this->_getParam('id'));
        if (empty($category)) {
            throw new Application_Model_Blog_Exception("分类不存在.");
        }

        try {
            $category->delete();
            $this->_flashMessenger->addMessage("分类删除成功.");
        } catch (Exception $e) {
            $this->_flashMessenger->addMessage("分类删除失败, " . $e->getMessage());
        }

        $this->_redirect(
        $this->view->url2(array('action'=>'list-category')));
    }

    public function addPostAction()
    {
        if ($this->getRequest()->isPost()) {
            $data = $this->getRequest()->getPost();
            var_dump($data);
            exit;
        }
        
        $this->view->categories = $this->categoryResource->fetchAll();
    }

    public function editPostAction()
    {
        if ($this->getRequest()->isPost()) {
            
        }
        
        $this->view->category = $this->categoryResource->fetchAll();
    }

}
